#include <iostream>  
#include <list>  
#include <vector>
#include <algorithm>

using namespace std;

void test_01()
{
    std::list<int> mylist;

    // 向列表中添加元素  
    mylist.push_back(10);
    mylist.push_front(20);
    mylist.push_back(30);

    // 使用迭代器遍历列表  
    for (auto it = mylist.begin(); it != mylist.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 删除元素  
    mylist.pop_front();
    mylist.pop_back();

    // 再次遍历列表  
    for (auto it = mylist.begin(); it != mylist.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

}

void test_02()
{
    list<int> lt;
    lt.push_back(1);
    lt.push_back(2);
    lt.push_back(3);
    lt.push_back(4);

    for (int e : lt)
        cout << e << " ";
    cout << endl;

    //在第二个元素后插入一个新元素
    auto iter = lt.begin();
    for (int i = 0; i < 2; i++)
    {
        ++iter;
    }

    lt.insert(iter, 20);


    for (int e : lt)
        cout << e << " ";
    cout << endl;

}

void test_03()
{
    list<int> lt;
    lt.push_back(1);
    lt.push_back(2);
    lt.push_back(3);
    lt.push_back(4);

    auto iter = lt.begin();
    while (iter != lt.end())
        ++iter;

    lt.erase(iter);
    cout << *iter;
}

void test_04()
{
    srand(time(0));
    const int N = 100000;
    vector<int> v;
    v.reserve(N);
    list<int> lt1;
    list<int> lt2;

    for (int i = 0; i < N; ++i)
    {
        auto e = rand();
        lt2.push_back(e);
        lt1.push_back(e);
    }


    // 拷贝到vector排序，排完以后再拷贝回来
    int begin1 = clock();
    // 先拷贝到vector
    for (auto e : lt1)
    {
        v.push_back(e);
    }

    // 排序
    sort(v.begin(), v.end());

    // 拷贝回去
    size_t i = 0;
    for (auto& e : lt1)
    {
        e = v[i++];
    }

    int end1 = clock();

    int begin2 = clock();
    lt2.sort();
    int end2 = clock();

    printf("vector sort:%d\n", end1 - begin1);
    printf("list sort:%d\n", end2 - begin2);
}

#include "list.h"

int main()
{
    xyx::test_list4();
    return 0;
}